﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SO_DataFetcher.Forms;
using SO_DataFetcher.PubFunctions;
using SO_DataFetcher.PubFunctions.Actions;
using SO_DataFetcher.PubFunctions.DataLink;
using SO_DataFetcher.PubFunctions.Paraments;
using SO_DataFetcher.PubFunctions.SqlScripts;

namespace SO_DataFetcher
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            PubControls.LogRichTextBox = this.richTextBox1;
            PubControls.LabelToolStatus = this.toolStatusLabel;
            PubControls.ProgressBarToolStrip = this.toolProgressBar;
            PubControls.DataGridViewActions = this.dataGridView1;

            PubControls.InitialDataGridView(this.dataGridView1);

            PubDataLinks.Init();
            PubParament.Init();
            PubScripts.Init();
            PubAction.Init();
        }

  
        private void toolbtnDataLink_Click(object sender, EventArgs e)
        {
            var frmDbLinkManager = new FrmDbLinkManager();
            frmDbLinkManager.ShowDialog(this);
        }

        private string _type = string.Empty;

        private string _ActionId = string.Empty;

        private void FrmMain_Load(object sender, EventArgs e)
        {
            if (PubAction.DataTableActions != null)
            {
                RefreshDataGridViewSource();
            }
        }

        private void RefreshDataGridViewSource()
        {
            DataTable dataTable = PubAction.DataTableActions.Copy();

            dataTable.Columns.Add("Select", Type.GetType("System.Boolean"));

            this.dataGridView1.DataSource = dataTable;
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            if (e.ColumnIndex == dataGridView1.Columns.Count-1)
            {
                bool isChecked = !(bool)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue;
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = isChecked;

                string index = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                if (isChecked)
                {  
                    if (PubAction.Actions.ContainsKey(index))
                    {
                        if (!PubAction.ActionList.ContainsKey(index))
                        {
                            PubAction.ActionList.Add(index,PubAction.Actions[index]);
                            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Orange;
                        }
                    }
                }
                else
                {
                    if (PubAction.ActionList.ContainsKey(index))
                    {
                        PubAction.ActionList.Remove(index);
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Gray;
                    }
                }
            }
            else if (e.ColumnIndex == 0)
            {
                this._ActionId = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
        }

        private void toolBtnRun_Click(object sender, EventArgs e)
        {
            PubAction.DoActions();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            if (e.ColumnIndex == 0)
            {
                this._ActionId = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                _type = "Edit";
                ShowAction();
            }
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _ActionId = string.Empty;
            _type = "Add";

            ShowAction();
        }


        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _type = "Edit";
            ShowAction();
        }



        private void ShowAction()
        {
            FrmAction frmAction = new FrmAction(_ActionId, _type);
            if (frmAction.ShowDialog(this) == DialogResult.OK)
            {
                ActionStruct action = frmAction.TempAction;
                if (!string.IsNullOrEmpty(action.Index))
                {
                    SaveActions(action);

                    RefreshDataGridViewSource();
                }
            }
        }

        /// <summary>
        /// 更新内存结构和xml文件
        /// </summary>
        /// <param name="dataLink"></param>
        private void SaveActions(ActionStruct action)  
        {
            string index = action.Index;

            DataRow[] datarows = PubAction.DataTableActions.Select("Index='" + index + "'");

            if (datarows.Length > 0)
            {
                DataRow dataRow = datarows[0];
                dataRow["Index"] = action.Index;
                dataRow["Name"] = action.Name;
                dataRow["DataLink"] = action.DataLink;
                dataRow["SQL"] = action.Sql;
                dataRow["OutPath"] = action.OutPath;
            }
            else
            {
                DataRow dataRow = PubAction.DataTableActions.NewRow();
                dataRow["Index"] = action.Index;
                dataRow["Name"] = action.Name;
                dataRow["DataLink"] = action.DataLink;
                dataRow["SQL"] = action.Sql;
                dataRow["OutPath"] = action.OutPath;
                PubAction.DataTableActions.Rows.Add(dataRow);
            }

            PubAction.SaveDataSetActions(); 
        }

        private void toolScriptsManager_Click(object sender, EventArgs e)
        {
            FrmScriptManager frmScriptManager = new FrmScriptManager();
            frmScriptManager.ShowDialog(this);
        }
        #region tips

        //private void InitialToolTip()
        //{
        //    this.dataGridView1.ShowCellToolTips = false;
        //    this.toolTip1.AutomaticDelay = 0;
        //    this.toolTip1.OwnerDraw = true;
        //    this.toolTip1.ShowAlways = true;
        //    this.toolTip1.ToolTipTitle = "动作参数: ";
        //    this.toolTip1.UseAnimation = true;
        //    this.toolTip1.UseFading = false;
        //}


        //private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        //{
        //    if (e.RowIndex < 0 || e.ColumnIndex < 0)
        //    {
        //        this.toolTip1.Hide(this.dataGridView1);
        //        return;
        //    }

        //    this.toolTip1.Hide(this.dataGridView1);

        //    if (e.ColumnIndex == 0)
        //    {
        //        string index = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();

        //        Point mousePos = PointToClient(MousePosition);

        //        this._mouseEnterRowindex = e.RowIndex;

        //        Console.WriteLine(mousePos);
        //        string tip = "Tip is " + GetTipString(index);
        //        this.toolTip1.Show(tip, this.dataGridView1, mousePos);
        //    }
        //    else
        //    {
        //        this.toolStrip1.Hide();
        //    }
        //}

        //private int _mouseEnterRowindex;

        //private string GetTipString(string id)
        //{
        //    string tip = "无内容";

        //    DataGridViewRow dataGridViewRow = this.dataGridView1.Rows[_mouseEnterRowindex];

        //    for(int i =1;i<this.dataGridView1.Columns.Count-1;i++)
        //    {
        //        string columnName = this.dataGridView1.Columns[i].HeaderText;

        //        if (i == 1)
        //            tip = columnName + ":" + dataGridViewRow.Cells[i].Value.ToString();
        //        else
        //            tip = tip + "\r\n" + columnName + ":" + dataGridViewRow.Cells[i].Value.ToString();
        //    }

        //    return tip;
        //}

        //private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
        //{
        //    e.Graphics.FillRectangle(
        //    Brushes.AliceBlue,
        //    e.Bounds);

        //    e.Graphics.DrawRectangle(
        //        Pens.Chocolate,
        //        new Rectangle(
        //            0,
        //            0,
        //            e.Bounds.Width - 1,
        //            e.Bounds.Height - 1));

        //    e.Graphics.DrawString(
        //    this.toolTip1.ToolTipTitle + e.ToolTipText,
        //    e.Font,
        //    Brushes.Red,
        //    e.Bounds);
        //}
        #endregion 
    }
}
